#!/usr/bin/env bash

# leah.sh: Language Editor and Helper

# This program is part of Faridayix, an artificially intelligent computer
# operating system.

# Copyright (C) 2010 Scott C. MacCallum
# scottmaccal@gmail.com

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

function c_language_template {
  printf "#include <stdio.h>" >> $file_name.c
  printf "\n" >> $c_file_name.c
  printf "int main()" >> $file_name.c
  printf "{" >> $file_name.c
  printf "\n\n" >> $file_name.c
  printf "return 0;" >> $file_name.c
  printf "\n" >> $file_name.c
  printf "}" >> $file_name.c

  printf "\n"

  printf "File: $file_name.c was successfully created."

  if [[ "$answer" == "no" || "$answer" == "n" ]]; then
    printf "Acknowledged."

    else
      printf "Please answer me with a (y)es or (n)o."
      printf "\n"

      c_language_template
  fi
}

function c_language {
  printf "\n"

  nl --body-numbering=a --number-width=1 -s " " $file_name.c

  printf "\n"

  printf "What line would you like me to look at? "

  read line_number

  printf "\n"

  printf "Would you like me to edit this line or insert an empty line above it? "

  read line_option

  if [[ "$line_option" == "edit" || "$line_option" == "e" ]]; then
    printf "\n"

    printf "What C function would you like me to replace line $line_number with? "

    read c_function
  fi

  if [[ "$line_option" == "insert" || "$line_option" == "i" ]]; then
    printf ""
  fi

  if [ "$c_function" == "printf" ]; then
    printf "\n"

    sed -e "$line_number i printf(\"\");" $file_name.c | nl --body-numbering=a --number-width=1 -s " "

    printf "\n"

    printf "What would you like me to add between the quotation marks? "

    read characters

    printf "\n"

    sed -e "$line_number i printf(\"$characters\");" $file_name.c | nl --body-numbering=a --number-width=1 -s " "

    sed -e "$line_number i printf(\"$characters\");" $file_name.c > temporary

    sed -e '/^$/d' temporary > $file_name.c

    rm temporary
  fi
}

function english_language {
  printf "English."
}

function main {
  printf "Would you like me to create a new file? "

  if [[ "$answer" == "yes" || "$answer" == "y" ]]; then
    printf "\n"

    printf "What language will I be helping you write? "

    read language
  
    if [ "$language" == "c" ]; then
      c_language
    fi

      if [ "$language" == "english" ]; then
        english_language
      fi
  fi
  
  if [[ "$answer" == "no" || "$answer" == "n" ]]; then
    printf "What is the name of the file that you would like me to visit? "

    read file_name
  
    else
      printf "Please answer me with [y]es or [n]o. "

      main
  fi
}

c_language {
  printf "What would you like me to name this file (please do not add the .c extension\n"
  printf "at the end)? "

  read file_name

  printf "\n"

  printf "Would you like me to create a C source code file with the standard input output\n"
  printf "header and main function? "

  read answer

  if [[ "$answer" == "no" || "$answer" == "n" ]]; then
    printf "Acknowledged."

  else
    printf "Please answer me with a (y)es or (n)o."
    printf "\n"

    c_language
  fi
}

printf "\n\n"

printf "Would you like me to help you modify this file? "

read answer

if [[ "$answer" == "yes" || "$answer" == "y" ]]; then
  c_language
fi

while [[ "$answer" == "yes" || "$answer" == "y" ]]; do
  printf "\n"

  printf "Would you like to continue editing this file? "

  read answer

  if [[ "$answer" == "yes" || "$answer" == "y" ]]; then
    c_language
  fi
done

printf "\n"

printf "Would you like me to apply a common C programming style to $file_name.c? "

read answer

printf "\n"

if [[ "$answer" == "yes" || "answer" == "y" ]]; then
  printf "What common C programming style would you like me to apply? "

  read programming_style
fi

if [ "$programming_style" == "gnu" ]; then
  indent -gnu $file_name.c

  nl --body-numbering=a --number-width=1 -s " " $file_name.c > $file_name-numbered-lines
fi

nl --body-numbering=a --number-width=1 -s " " $file_name.c > $file_name-numbered-lines

printf "Would you like me to compile this program with standard options? "

read answer

printf "\n"

if [[ "$answer" == "yes" || "$answer" == "y" ]]; then
  gcc --verbose -o $file_name $file_name.c
fi

printf "\n"

printf "Would you like me to exectute $file_name? "

read answer

if [[ "$answer" == "yes" || "$answer" == "y" ]]; then
  ./$file_name
fi
# leah.sh
# END OF FILE